package com.cskaoyan.mall.service;

import com.cskaoyan.mall.dao.GoodsDao;
import com.cskaoyan.mall.dao.GoodsDaoImpl;
import com.cskaoyan.mall.dao.UserDao;
import com.cskaoyan.mall.dao.UserDaoImpl;
import com.cskaoyan.mall.model.goods.*;
import com.cskaoyan.mall.model.message.Reply;
import com.cskaoyan.mall.model.message.ReplyMsgVO;
import com.cskaoyan.mall.util.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: jyf2000jyf
 */
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<Type> getType() {
        return goodsDao.getType();
    }

    @Override
    public boolean addGoods(AddGoodsBO addGoodsBO) {
        List<AddAndUpdateGoodsSpecBO> addAddAndUpdateGoodsSpecBOList = addGoodsBO.getSpecList();
        Integer stockNum = addAddAndUpdateGoodsSpecBOList.get(0).getStockNum();
        Double unitPrice = addAddAndUpdateGoodsSpecBOList.get(0).getUnitPrice();
        for (int i = 1; i < addAddAndUpdateGoodsSpecBOList.size(); i++) {
            if (addAddAndUpdateGoodsSpecBOList.get(i).getStockNum() > stockNum) {
                stockNum = addAddAndUpdateGoodsSpecBOList.get(i).getStockNum();
            }
            if (addAddAndUpdateGoodsSpecBOList.get(i).getStockNum() < unitPrice) {
                unitPrice = addAddAndUpdateGoodsSpecBOList.get(i).getUnitPrice();
            }
        }
        Goods goods = new Goods(addGoodsBO, stockNum, unitPrice);
        if (!goodsDao.addGoods(goods)) {
            return false;
        }
        // 先执行上面的代码，一定匹配刚加入的
        int goodsId = goodsDao.selectMaxGoodsId();
        List<Spec> specList = new ArrayList<>();
        for (AddAndUpdateGoodsSpecBO addAddAndUpdateGoodsSpecBO : addAddAndUpdateGoodsSpecBOList) {
            Spec spec = new Spec(addAddAndUpdateGoodsSpecBO, goodsId);
            specList.add(spec);
        }
        return goodsDao.addSpecList(specList);
    }

    @Override
    public List<GoodsByTypeVO> getGoodsByType(int typeId) {
        if (typeId == -1) {
            return goodsDao.getGoodsOrderByDesc();
        }
        return goodsDao.getGoodsByType(typeId);
    }

    @Override
    public boolean addType(Type type) {
        return goodsDao.addType(type);
    }

    @Override
    public boolean deleteGoods(int id) {
        return goodsDao.deleteGoodsById(id) & goodsDao.deleteSpecByGoodsId(id);
    }

    @Override
    public boolean deleteType(int typeId) {
        return goodsDao.deleteType(typeId) & goodsDao.deleteSpecByTypeId(typeId) & goodsDao.deleteGoodsByTypeId(typeId);
    }

    @Override
    public GoodsInfoVO getGoodsInfo(int goodsId) {
        List<Spec> specList = goodsDao.getSpecList(goodsId);
        Goods goods = goodsDao.getGoodsVOByGoodsId(goodsId);
        GoodsVO goodsVO = new GoodsVO(goods);
        return new GoodsInfoVO(specList, goodsVO);
    }

    @Override
    public boolean updateGoods(UpdateGoodsBO updateGoodsBO) {
        Goods goods = new Goods(updateGoodsBO, null, null);
        int goodsId = updateGoodsBO.getId();
        // 修改的方法是先将原来的所有spec删除，再添加一份新的，其中一定要保证删除正常执行，因此作为if条件判断
        if (goodsDao.deleteSpecByGoodsId(goodsId)) {
            List<Spec> specList = new ArrayList<>();
            List<AddAndUpdateGoodsSpecBO> addAddAndUpdateGoodsSpecBOList = updateGoodsBO.getSpecList();
            for (AddAndUpdateGoodsSpecBO addAddAndUpdateGoodsSpecBO : addAddAndUpdateGoodsSpecBOList) {
                Spec spec = new Spec(addAddAndUpdateGoodsSpecBO, goodsId);
                specList.add(spec);
            }
            if (goodsDao.addSpecList(specList)) {
                goodsDao.updateStockNumAndPrice(goodsId);
                return goodsDao.updateGoods(goods, goodsId);
            }
        }
        return false;
    }

    @Override
    public boolean addSpec(Spec spec) {
        return goodsDao.addSpec(spec);
    }

    @Override
    public boolean deleteSpec(Spec spec) {
        return goodsDao.deleteSpec(spec);
    }

    @Override
    public List<ReplyMsgVO> getNoReplyMsg() {
        return goodsDao.getNoReplyMsg();
    }

    @Override
    public List<ReplyMsgVO> getRepliedMsg() {
        return goodsDao.getRepliedMsg();
    }

    @Override
    public boolean reply(Reply replyContent) {
        return goodsDao.reply(replyContent);
    }

    @Override
    public List<GoodsByNameVO> getGoodsByName(String goodsName) {
        return goodsDao.getGoodsByName(goodsName);
    }

    @Override
    public List<GoodsMsgVO> getGoodsMsg(Integer id) {
        return goodsDao.getGoodsMsg(id);
    }

    @Override
    public GoodsCommentVO getGoodsComment(Integer goodsId) {
        // TODO
        List<GoodsCommentVOInnerComment> goodsCommentVOInnerCommentList = goodsDao.getGoodsComment(goodsId);
        if(goodsCommentVOInnerCommentList.size()==0){
            return null;
        }
        // i用于记录打分的次数
        int i = 0;
        // time用于记录好评的次数
        int time = 0;
        for (; i < goodsCommentVOInnerCommentList.size(); ) {
            if (goodsCommentVOInnerCommentList.get(i).getScore() == 0) {
                break;
            }
            if (goodsCommentVOInnerCommentList.get(i).getScore() == 100) {
                time++;
            }
            i++;
        }
        Double rate = 100.0 * time / i;
        return new GoodsCommentVO(goodsCommentVOInnerCommentList, rate);
    }

    @Override
    public GoodsInfoWithoutGoodsVO getGoodsInfoWithoutGoodsVO(Integer goodsId) {
        List<Spec> specList = goodsDao.getSpecList(goodsId);
        Goods goods = goodsDao.getGoodsVOByGoodsId(goodsId);
        // GoodsVO完成了domain的设置，这里没有GoodsVO，因此需要设置一下img
        goods.setImage(Constant.domain + goods.getImage());
        return new GoodsInfoWithoutGoodsVO(specList, goods);
    }

    @Override
    public boolean askGoodsMsg(AskGoodsMsgBO askGoodsMsgBO) {
        String goodsName = goodsDao.getGoodsNameByID(Integer.parseInt(askGoodsMsgBO.getGoodsId()));
        Integer userId = userDao.getUserIdByUsername(askGoodsMsgBO.getToken());
        AskGoodsMsg askGoodsMsg = new AskGoodsMsg(userId,askGoodsMsgBO.getToken(),Integer.parseInt(askGoodsMsgBO.getGoodsId()),goodsName,askGoodsMsgBO.getMsg());
        return goodsDao.askGoodsMsg(askGoodsMsg);
    }
}
